package yhp.com.zijie.dao.impl;

import yhp.com.zijie.bean.Menu;
import yhp.com.zijie.bean.Role;
import yhp.com.zijie.bean.Users;
import yhp.com.zijie.dao.RoleDao;
import yhp.com.zijie.util.DBUtils;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class RoleDaoImpl extends DBUtils implements RoleDao {

    @Override
    public List<Role> getRoles(int pageIndex, int pageSize) {
        List<Role> roleList = new ArrayList<>();

        try {
            String sql = "SELECT * from role limit ?,?";
            List params = new ArrayList();
            params.add((pageIndex-1)*pageSize);
            params.add(pageSize);
            resultSet = query(sql, params);

            while (resultSet.next()){
                Role role = new Role();
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                roleList.add(role);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return roleList;
    }

    @Override
    public int total() {
        int total = 0;

        try {
            String sql = "select count(*) from role";

            resultSet = query(sql, null);

            while (resultSet.next()){
                total = resultSet.getInt(1);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }

        return total;
    }

    @Override
    public int insertRole(Role role) {

        int key = 0;
        try {
            String sql = "insert into role values(null,?,?)";

            List params= new ArrayList();
            params.add(role.getRoleName());
            params.add(role.getRoleState());
            int update = update(sql, params);

            ResultSet generatedKeys = pps.getGeneratedKeys();
            if (generatedKeys.next()){
                key = generatedKeys.getInt(1); //新增的数据的id值
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeAll();
        }

        return key;
    }

    @Override
    public Role findById(int roleId) {

        Role role = new Role();//角色-》多个菜单
        List<Menu> menuList = new ArrayList<>();
        try {
            String sql = "SELECT * from role r , menu n , middle m where r.roleid=m.roleid and m.menuid=n.menuid and r.roleid=?";

            List param = new ArrayList();
            param.add(roleId);
            resultSet = query(sql, param);


            while (resultSet.next()){
                role.setRoleId(resultSet.getInt("roleid"));
                role.setRoleName(resultSet.getString("rolename"));
                role.setRoleState(resultSet.getInt("rolestate"));

                Menu menu = new Menu();
                menu.setMenuId(resultSet.getInt("menuid"));
                menu.setMenuName(resultSet.getString("menuname"));
                menu.setUpMenuId(resultSet.getInt("upmenuid"));
                menu.setDesc(resultSet.getString("desc"));
                menu.setUrl(resultSet.getString("url"));
                menu.setState(resultSet.getInt("state"));

                menuList.add(menu);
            }
            role.setMenuList(menuList);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            closeAll();
        }


        return role;
    }













}
